草庐IT

CountDownLatch和ExecutorService 线程池cachedThreadPool.submit

全部标签

Java线程自发唤醒

我有一个Java线程做这样的事情:while(running){synchronized(lock){if(nextVal==null){try{lock.wait();}catch(InterruptedExceptionie){continue;}}val=nextVal;nextVal=null;}...dostuffwith'val'...}我在其他地方设置的值是这样的:if(val==null){LOG.error("nullvalue");}else{synchronized(lock){nextVal=newVal;lock.notify();}}偶尔(字面上每几百万次一

java - Swing 线程安全编程

publicstaticvoidmain(Stringargs[]){/*SettheNimbuslookandfeel*////*IfNimbus(introducedinJavaSE6)isnotavailable,staywiththedefaultlookandfeel.*Fordetailsseehttp://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html*/try{for(javax.swing.UIManager.LookAndFeelInfoinfo:javax.swing.UIMana

java - 如何从java中的另一个正在运行的线程访问方法

我是Java线程的新手。我想要做的是从ThreadB对象获得对当前正在运行的线程ThreadA的实例的访问权限,并调用其名为setSomething的方法。1)我认为我比实际更努力2)我有一个空指针异常,所以我一定是在访问该方法时做错了什么这是我目前所了解的,我已经尽职调查并在StackOverflow上查看了类似的问题。我有一个当前线程在后台运行://assumethisthreadiscalledbysomeotherapplicationpublicclassThreadAimplementsRunnable{privateThreadaThread;publicThreadA(

java - 如何在 Java 中对返回 boolean 值的两个并行线程执行短路评估?

我正在寻找逻辑上等同于以下问题的指导:publicbooleanparallelOR(){ExecutorServiceexecutor=Executors.newFixedThreadPool(2);FuturetaskA=executor.submit(newSlowTaskA());FuturetaskB=executor.submit(newSlowTaskB());returntaskA.get()||taskB.get();//ThisisnotwhatIwant//Exceptionhandlingomittedforclarity}上面的构造给出了正确的结果但是总是等待

java - 使用 Guava CharMatcher 作为类中的静态字段。 CharMatcher 线程安全吗?

我需要替换字符串中的一些字符:每个点加下划线。只需执行:myString.replace(".","_");哪个有效。不过,我想使用Guava的CharMatcher,它应该有更好的性能。CharMatcherdotCharMatcher=CharMatcher.anyOf(".");dotCharMatcher.replaceFrom(myString,"_");它运行在一个有很多线程的服务器上。我能否在使用它的类中将dotCharMatcher设为静态字段,还是应该在每个请求中创建一个?(它是线程安全的吗?)谢谢 最佳答案 是的

java - javax.servlet.ServletContext set/getAttribute 线程安全吗?

必须在javax.servlet.ServletContext中使用setAttribute()和getAttribute(String),我找不到任何关于预期的信息并发访问的行为。但是,这些操作很可能会被不同的线程调用。servletspecification3.0状态:Aservletcanbindanobjectattributeintothecontextbyname.AnyattributeboundintoacontextisavailabletoanyotherservletthatispartofthesameWebapplication.但是,没有关于这些操作的并发行

java - 如何使用 ExecutorService 轮询直到结果到达

我有一个场景,我必须轮询远程服务器以检查任务是否已完成。完成后,我会进行不同的调用以检索结果。我最初认为我应该使用SingleThreadScheduledExecutor和scheduleWithFixedDelay进行轮询:ScheduledExecutorServiceexecutor=Executors.newSingleThreadScheduledExecutor();ScheduledFuturefuture=executor.scheduleWithFixedDelay(()->poll(jobId),0,10,TimeUnit.SECONDS);publicvoidp

java - KeyGenerator 线程安全吗?

我想知道我是否可以实例化javax.crypto.KeyGenerator仅一次,然后在多线程环境中使用此实例。它的JavaDoc文档没有说明它的线程安全性。或者使用ThreadLocal会更好方法?更新:一个相关的问题是IsSecureRandomthreadsafe?虽然JavaDoc没有声明该类是线程安全的,但社区仍然认为它是线程安全的决定从实践的角度来看非常重要。我想知道KeyProvider的情况。 最佳答案 除非文档明确保证线程安全,否则将任何事物视为非线程安全。你是对的,这种哲学对线程安全文档的稀缺性几乎没有帮助...

java - 按特定顺序仅使用 5 个线程打印从 1 到 10 的整数

这个问题在这里已经有了答案:Print1to100using10threadsinjava(12个答案)关闭4年前。我最近开始使用Java进行多线程我在解决一个只有5个线程的问题时遇到了问题,范围从T1、T2、...T5。任务是按以下顺序打印从1到10的数字。T1->1T2->2T3->3T4->4T5->5T1->6T2->7T3->8T4->9T5->10我尝试用这段代码解决了它。publicstaticvoidmain(String[]args)throwsInterruptedException{Countercounter=newCounter();Thread[]tArra

java - 使用 JMX 获取线程 CPU 时间的有效方法

我目前通过以下方式使用JMX获取总线程CPU时间:privatelongcalculateTotalThreadCpuTime(ThreadMXBeanthread){longtotalTime=0l;for(ThreadInfothreadInfo:thread.dumpAllThreads(false,false))totalTime+=thread.getThreadCpuTime(threadInfo.getThreadId());returntotalTime;}由于ThreadMXBean实际上是一个远程代理,性能是可怕的,对于这个实际的方法调用来说是秒的数量级。有没有更快